package com.abeir.bilibili.detection;

import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FullFolderDetector implements Detector<Path> {

    private final Path rootPath;

    public FullFolderDetector(Path root){
        rootPath = root;
    }

    @Override
    public List<Path> detect() {
        File[] dirs = rootPath.toFile().listFiles(d -> d.isDirectory() && isValid(d));
        if(dirs == null || dirs.length == 0){
            return List.of();
        }
        return Arrays.stream(dirs).map(File::toPath).collect(Collectors.toList());
    }

    private boolean isValid(File dir){
        String[] ls = dir.list((File d, String n) -> n.endsWith(".m4s") || n.equals("videoInfo.json"));
        return ls != null && ls.length == 3;
    }
}
